package org.jeecg.modules.zixishi.service.impl;

import cn.hutool.core.date.DateUnit;
import org.apache.commons.lang3.StringUtils;
import org.checkerframework.checker.units.qual.A;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.system.util.DateUtil;
import org.jeecg.modules.zixishi.entity.Quyu;
import org.jeecg.modules.zixishi.entity.Seat;
import org.jeecg.modules.zixishi.mapper.QuyuMapper;
import org.jeecg.modules.zixishi.mapper.SeatMapper;
import org.jeecg.modules.zixishi.service.ISeatService;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @Description: 座位
 * @Author: jeecg-boot
 * @Date:   2024-05-19
 * @Version: V1.0
 */
@Service
public class SeatServiceImpl extends ServiceImpl<SeatMapper, Seat> implements ISeatService {
	
	@Autowired
	private SeatMapper seatMapper;
	@Autowired
	private QuyuMapper quyuMapper;
	
	@Override
	public List<Seat> selectByMainId(String mainId) {
		return seatMapper.selectByMainId(mainId);
	}

	@Override
	public Map seatPrice(String quyuId,String reserveType,String startTime,String endTime) throws Exception{
		Quyu quyu = quyuMapper.selectById(quyuId);
		Map<String, Object> retMap = new HashMap<>();
		if (StringUtils.equals(reserveType, "datetime")) {
			Double price = Double.parseDouble(quyu.getTimePrice());
			int hours = DateUtil.hoursDiff(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(startTime), new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(endTime));
			long mins = cn.hutool.core.date.DateUtil.between(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(startTime), new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(endTime), DateUnit.MINUTE);
			System.out.println(mins);
			Double total = (price/60)*mins;
			retMap.put("hours", hours);
			retMap.put("price", price);
			retMap.put("total", String.format("%.2f", total));
		}else{
			Double price = Double.parseDouble(quyu.getDayPrice());
			long days = cn.hutool.core.date.DateUtil.betweenDay(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(startTime),new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(endTime),false);
//			int days = DateUtil.daysDiff(new SimpleDateFormat("yyyy-MM-dd").parse(startTime), new SimpleDateFormat("yyyy-MM-dd").parse(endTime));
			days+=1;
			retMap.put("hours", days);
			retMap.put("price", price);
			retMap.put("total", String.format("%.2f", days * price));
		}
		return retMap;
	}
}
